package eu.goldenak.ircbot.module;

import eu.goldenak.ircbot.*;
import java.util.*;

public class CmdHandler extends ModuleBase {
	/**
	 * The configuration for this module.
	 */
	private HashMap<String, String> m_hmConfig;
	
	/**
	 * The constructor loads in the configuration for us.
	 */
	public CmdHandler() throws Exception {
		m_hmConfig = ConfigManager.getModule ("CmdHandler");
	}
	
	/**
	 * This is where it all happens, the commands get parsed in here.
	 */
	public void onChannelPrivmsg (IrcBot pBot, String sChannel, String sNickname, String sMessage) {
		if (pBot.in.user.equals (m_hmConfig.get ("botowner"))) {
			if (sMessage.equals ("!shutdown")) {
				BotGroup pBots = BotManager.getInstance ().getBots ();
				
				for (IrcBot pIrcBot : pBots) {
					pIrcBot.out.quit ("Shutdown requested by " + sNickname);
				}
			} else if (sMessage.startsWith ("!gc")) {
				pBot.out.privmsg (sChannel, "Running garbage collector...");
				
				System.gc ();
				
				pBot.out.privmsg (sChannel, "Garbage collection complete.");
			} else if (sMessage.startsWith ("!botlist")) {
				StringBuilder sOutput = new StringBuilder ("Bots running: ");
				for (IrcBot pIrcBot : BotManager.getInstance ().getBots ()) {
					sOutput.append (pIrcBot.getNickname () + ", ");
				}
				
				sOutput.delete (sOutput.length () - 2, sOutput.length ());
				pBot.out.privmsg (sChannel, sOutput.toString ());
			}
		}
		
		if (sMessage.equals ("!nanotime")) {
			pBot.out.privmsg (sChannel, "3Nanotime: " + System.nanoTime ());
		} else if (sMessage.startsWith ("!hax")) {
			if (sMessage.trim ().length () > 4) {
				String sOut = sMessage.substring (5);
				
				pBot.out.privmsg (sChannel, "Okay! " + sOut);
			} else {
				pBot.out.privmsg (sChannel, "Oetlul!");
			}
		}
	}
	
	public void run() {
		/*long lastCall = new Date().getTime();
		int i = 0;
		
		while (true) {
			if (new Date().getTime() - lastCall >= 5000) {
				lastCall = new Date().getTime();
				bots[i++].getIrcHandler().privmsg("#goldenak.test", "Hai. Number: " + i);
				
				if (i == bots.length) i = 0;
			}
			
			try {
				Thread.sleep(500);
			} catch (Exception e) {}
		}*/
	}
}